<?php
class DataTableRendererHTMLList extends DataTableRenderer
{

	private function printYear($date)
	{
		return "<td class=\"EventReportYear\" colspan=\"4\">".$date->format("Y")."</td></tr><tr>";
	}

	private function printMonth($date)
	{
		return "<td class=\"EventReportMonth\" colspan=\"4\">".$date->format("F")."</td></tr><tr>";
	}

	private function printDay($date)
	{
		return "<td class=\"EventReportDay\" colspan=\"4\">".$date->format("j l")."</td></tr><tr>";
	}

	public function getHTML()
	{
		$html = "<SCRIPT language='javascript'>\n";
		$html .= "function ".$this->dataTable->getName()."Refresh()\n";
		$html .= "{\n";
		$html .= "document.getElementById(\"Action\").value = \"EventReport\";\n";
		$html .= "document.getElementById(\"pageForm\").submit();\n";
		$html .= "}\n";
		$html .= "</SCRIPT>\n";


		$RecordID = new Hidden();
		$RecordID->setName("RecordID");
		$html .= $RecordID->getHTML();

		$DBTableName = new Hidden();
		$DBTableName->setName("DBTableName");
		$html .= $DBTableName->getHTML();

		$html .= "<table>";
/*		
		$conditions = "";
		$dtNow = new DateTime("now");
		$showAfter = (isset($_POST["dtShowAfter"]))?$_POST["dtShowAfter"]:$dtNow->format("Y-m-d H:i");
		$showBefore = (isset($_POST["dtShowBefore"]))?$_POST["dtShowBefore"]:$dtNow->format("Y-m-d H:i");

		$conditionDeleted = ($this->dataTable->getShowDeleted())?"":" DeletedBy = ''";
		if($this->dataTable->getShowBefore() && $this->dataTable->getShowAfter())$conditionTime = "";
		if(!$this->dataTable->getShowBefore() && $this->dataTable->getShowAfter())$conditionTime = "End >= '".substr($showAfter,0,10)."' ";
		if($this->dataTable->getShowBefore() && !$this->dataTable->getShowAfter())$conditionTime = "Start <= '".substr($showBefore,0,10)."' ";
		if(!$this->dataTable->getShowBefore() && !$this->dataTable->getShowAfter())$conditionTime = " 0 = 1";

		if (($conditionDeleted == "") && ($conditionTime == ""));
		if (($conditionDeleted != "") && ($conditionTime == ""))
			$this->addANDCondition($conditionDeleted);
		if (($conditionDeleted == "") && ($conditionTime != ""))
			$this->addANDCondition($conditionTime);
		if (($conditionDeleted != "") && ($conditionTime != ""))
		{
			$this->addANDCondition($conditionDeleted);
			$this->addANDCondition($conditionTime);
		}
		$ANDConditions = "";
		foreach ($this->ANDConditions as $condition)
		{
			$ANDConditions .= $condition." AND ";
		}
		$ANDConditions = substr($ANDConditions,0,-4);

		$ORConditions = "";
		foreach ($this->ORConditions as $condition)
		{
			$ORConditions .= $condition." OR ";
		}
		$ORConditions = substr($ORConditions,0,-3);


		$this->dataTable->setSQL($this->dataTable->getSQL()." WHERE (".$ANDConditions.") AND (".$ORConditions.") ");
		$tmp = $this->dataTable->getOrderBy();
		if(isset($tmp))
		{
			$tempSql = $this->dataTable->getSQL();
			$this->dataTable->setSQL($tempSql .= " ORDER BY ".$this->dataTable->getOrderBy());
		}
*/
		$this->dataTable->getDatabase()->ExecuteSQL($this->dataTable->getSQLCommand());
//		echo $this->dataTable->getSQLCommand();
/*		$html .= "<tr>";
		foreach($this->dataTable->getColumns() as $column)
		{
			$html .= "<th>".$column->getTitle()."</th>";
		}
		$html .= "</tr>";*/

		$previousDate = new DateTime("-10 years");
		while($row=$this->dataTable->getDatabase()->getRow())
		{
			echo mysql_error();
/*
			// if ShowEventTypes is set then check if row is relevent
			if($this->dataTable->getShowEventTypes())
			{
				if($_POST['dtEventType']!=$row['EventTypeID'])
					continue;
			}
			// if ShowLocations is set then check if this row is relevent
			if($this->dataTable->getShowLocations())
			{
				$locationSQL = "SELECT * from Locations where EventID=".$row['ColumnID']." AND ( ";
				foreach($_POST['dtLocation'] as $locationID)
				{
					$locationSQL .= "LocationNameID = ".$locationID." OR ";
				}
				$locationSQL = substr($locationSQL,0,-3).") ";
//				echo $locationSQL."<br>";
				$database = $this->dataTable->getDatabase()->Recopy();
				$database->ExecuteSQL($locationSQL);
				if(!$database->getRow())
					continue;
			}
			// if ShowInvolvees is set then check if this row is relevent
			if($this->dataTable->getShowEventInvolvees())
			{
				$involveeSQL = "SELECT * from EventInvolvees where EventID=".$row['ColumnID']." AND ( ";
				foreach($_POST['dtEventInvolvee'] as $involveeID)
				{
					$involveeSQL .= "EventInvolveeNameID = ".$involveeID." OR ";
				}
				$involveeSQL = substr($involveeSQL,0,-3).") ";
//				echo $locationSQL."<br>";
				$database = $this->dataTable->getDatabase()->Recopy();
				$database->ExecuteSQL($involveeSQL);
				if(!$database->getRow())
					continue;
			}
			*/
			$html .= "<tr>";
			foreach($this->dataTable->getColumns() as $column)
			{
//				$DeletedFontText = ($row["DeletedBy"]=='1')?" style=\"color: #dd0000\"":"";

				switch ($column->getDisplayAs())
				{
					case 'DateOnly':
						$date = new DateTime($row[$column->getField()]);
						if($date->format("Y")!=$previousDate->format("Y"))$html.=$this->printYear($date);
						if($date->format("Ym")!=$previousDate->format("Ym"))$html.=$this->printMonth($date);
						if($date->format("Ymd")!=$previousDate->format("Ymd"))$html.=$this->printDay($date);
						$previousDate = $date;
						$html .= "<td width=\"20px\"></td>";
//						$html .= "<td>".$date->format("D d M Y")."</td>";
						break;

					case 'TimeOnly':
						$date = new DateTime($row[$column->getField()]);
						$html .= "<td>".$date->format("H:i")."</td>";
						break;

					case 'DateTime':
						$date = new DateTime($row[$column->getField()]);
						$html .= "<td>".$date->format("d M Y  H:i")."</td>";
						break;

					case 'SingleEnumeration':
						$field = $column->getField();
						$sql = "select ".substr($field,0,-2)." FROM ".substr($field,0,-2)."s WHERE ".$field."=".$row[$field];
						$database = $this->dataTable->getDatabase()->Recopy();
						$database->ExecuteSQL($sql);
						$row1 = $database->getRow();
						$html .= "<td>".$row1[substr($field,0,-2)]."</td>";
						break;

					case 'MultiEnumeration':
						$field = $column->getField();
						$sql = "SELECT id.".$field."NameID, n.ShortName FROM ".$field."s as id INNER JOIN ".$field."Names as n ON id.".$field."NameID = n.".$field."NameID WHERE id.EventID = ".$row['ColumnID'];
//						echo $sql;
						$database = $this->dataTable->getDatabase()->Recopy();
						$database->ExecuteSQL($sql);
						$result = "";
						while($row1 = $database->getRow())
					{
							$result.=$row1["ShortName"]."&nbsp;";
//							print_r ($row1);
					}
						$html .= "<td>".$result."</td>";
						break;

					default:
						$html .= "<td>".$row[$column->getField()]."</td>";
						break;	
				}
			}
/*			if($this->dataTable->getAreEditable())
			{
				$editButton = new Button();
				$editButton->setName("editButton");
				$editButton->setValue("Edit");
				$editButton->setOnClick("editRecord('".$row['ColumnID']."','".$this->dataTable->getDBTableName()."');");
				$html .= "<td>".$editButton->getHTML()."</td>";
			}

			if($this->dataTable->getAreDeletable())
			{
				$deleteButton = new Button();
				$deleteButton->setName("deleteButton");
				$deleteButton->setValue("Delete");
				$deleteButton->setOnClick("deleteRecord('".$row['ColumnID']."','".$this->dataTable->getDBTableName()."');");
				$html .= "<td>".$deleteButton->getHTML()."</td>";
			}
*/
			$html .= "</tr>\n";

		}

/*		$newButton = new Button();
		$newButton->setName("newButton");
		$newButton->setValue("New");
		$newButton->setOnClick("newRecord('".$this->dataTable->getDBTableName()."');");
		$html .= "<tr><td>".$newButton->getHTML()."</tr></td>";
*/
		$html .= "</table>";

		$html .= "<p>";
/*
		$panel = new PanelFilterRequest();
		$panel->setParentPage($this->dataTable->getParentPage());
		$panel->setName("PanelFilter");
		$panel->showDeleted($this->dataTable->getShowDeleted());
		$panel->showBefore($this->dataTable->getShowBefore());
		$panel->showAfter($this->dataTable->getShowAfter());
		$panel->showLocations($this->dataTable->getShowLocations());
		$panel->showEventInvolvees($this->dataTable->getShowEventInvolvees());
		$panel->showEventTypes($this->dataTable->getShowEventTypes());
		$html .= $panel->getHTML();
*/
		$html .= "</p>";

		return $html;


	}

}

?>